/*
Copyright (C) 2021-2025 Casa Xu (also Zhiyan Xu) from HIT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/*
 * LOS_SYS.c
 *
 *  Created on: 2022年6月20日
 *      Author: Casa XU
 */

#include "LOS_SYS.h"

void LOS_SYS_Init()
{
    LOS_GPIO_Init();
    LOS_I2C_Init();
    LOS_CAN_Init();
    LOS_SPI_Init();
    LOS_UART_Init();
    LOS_UART_SetBaudrate(4,115200);
    LOS_PWM_Init();
    //软件看门狗初始化
    //LOS_WDT_DWDInit();
    //启动软件看门狗 
    //LOS_WDT_DWDStart();
    LOS_FATFS_Init();
    gioSetBit(gioPORTB, 2, 1);
    gioSetBit(gioPORTB, 3, 1);
    LOS_MAX14830_Init(LOS_MAX14830_NUM1);
    LOS_MAX14830_Init(LOS_MAX14830_NUM2);
    LOS_FotonAmur_InitStruct();
    LOS_STA_Init();
    LOS_FVAR_GetAllVAR();
    _enable_IRQ_interrupt_();
}

/** @fn void LOS_EnterAdminMode()
*   @brief 进入ARM特权模式
*   @note ARM的基本特性，从USR模式进入SWI模式
*/
void LOS_EnterAdminMode()
{
    register uint32_t u32Mode = 0x13ul; /* Supervisor */
    u32Mode = switchCpuMode(u32Mode);
    switchToSystemMode();
}

/** @fn void LOS_ExitAdminMode()
*   @brief 离开ARM特权模式
*   @note ARM的基本特性，从SWI模式回到USR模式
*/
void LOS_ExitAdminMode()
{
    switchToUserMode();
}


/** @fn void LOS_SelfCheck(void)
*   @brief OBC程序自检函数
*   @note 系统初始化完成后进行自检，用以验证在轨上注程序完整性，若函数可以正常退出，则说明程序完整，若跑飞则不完整或错误
*/
void LOS_SelfCheck(void)
{
    uint8_t selfcheck[8] = {0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95, 0x95};
    //LOS_UART_Transmit(4, "UARTSELFCHECK");                     //串口自检
    LOS_CAN_Transmit(LOS_CAN_NUM1, 0x06959595, selfcheck, 8);  //CAN自检
    LOS_SPI_WriteReadLastByte(LOS_SPI_NUM3,0x95,CS_NULL);      //
    LOS_WDT_Feed();
}



/** @fn void LOS_STA_Init(void)
*   @brief 单机状态量初始化
*   @note 
*/
void LOS_STA_Init(void)
{
    uint8_t cnt = 0;
    //平台
    
    LOS_SAT_SoftSafeMode = 0;
    LOS_SAT_SafeModeCNT = 0;
    LOS_TC_READ = 0;
    LOS_TC_EX = 0;
    LOS_TC_LASTCMD = 0;
    //测试模式标志
    LOS_TESTM_FLAG = 0;
    //测试模式时长
    LOS_TESTM_DUR = 0;

    //遥测周期控制
    LOS_TM_InvTime = 1;

    //请求测控工参允许/禁止
    TMTC_PERMITARG = 0XAA;
    
    LOS_PCDU_CMDCNT = 0;
    LOS_PCDU_LASTCMD = 0;
    LOS_TM_SATSta = 0;
    LOS_HFT_STA = 0;

    LOS_HL_RBTP = 40;
    LOS_HL_RECOVP = 100;

    CRC32_SD_OBCCAL = 0;
    CRC32_SD_SAVE = 0;
    //CRC32_TC_OBCCAL = 0;
    CRC32_SD_OFFSET = 0;
    CRC32_TC_TRAN = 0;

    LOS_UPLOAD_FileNum = 0x00;
    LOS_UPLOAD_CNT = 0;
    LOS_UPLOAD_BlockSize = TMTC2SD_UPFSIZE;
    LOS_UPLOAD_OffSet = 0;
    LOS_UPLOAD_BlockCnt = 0;
    LOS_UPLOAD_FileReceived = 0;

    STA_EPS.TC_LastTime = 0;
    STA_EPS.PD_LastTime = 0;

    LOS_TM_CMOSTRAN = 0;
    //姿态工作模式
//    MODE_AOCS.mode = Damping;
    MODE_AOCS.starttime = 0;
    //最近一条延时遥控指令
    LOS_TC_FirstDelayCMD = 0;
    //最近一条延时遥控指令执行时刻
    LOS_TC_FirstDelayTime = 0;
    //当前任务号
    LOS_TC_NOWTask = 0;
    //当前任务执行时刻
    LOS_TC_NOWTaskTime = 0;
    //待执行任务计数
    LOS_TC_TaskNum = 0;
    LOS_TC_LASTCMDTIME = 0;

    LOS_UP_BASEADDR = 0;

    LOS_TM_UnitSta[0] = 0XFF;
    LOS_TM_UnitSta[1] = 0XFF;
    LOS_TM_UnitSta[2] = 0XFF;

    //磁力矩器
    MTPOL_X1 = 0;
    MTPOL_Y1 = 0;
    MTPOL_Z1 = 0;
    MTPOL_X2 = 1;
    MTPOL_Y2 = 1;
    MTPOL_Z2 = 1;

    LOS_FW_PERMIT = 0XAA;
    LOS_MT_PERMIT = 0XAA;
    
    //存储板
    STA_SATA.CMD = 0;
    STA_SATA.HL_TXF = 1;
    STA_SATA.MSG = 1;
    STA_SATA.RBCNT = 0;
    STA_SATA.NFR = 0;
    STA_SATA.REC = 0;
    //测控板
    STA_TMTC.CMD = 0;
    STA_TMTC.HL_TXF = 1;
    STA_TMTC.MSG = 1;
    STA_TMTC.RBCNT = 0;
    STA_TMTC.NFR = 0;
    STA_TMTC.REC = 0;
    //数传基带
    STA_DT.CMD = 0;
    STA_DT.MSG = 1;
    //GNSS
    STA_GNSS.CMD = 0;
    STA_GNSS.HL_TXF = 1;
    STA_GNSS.MSG = 1;
    STA_GNSS.RBCNT = 0;
    STA_GNSS.NFR = 0;
    STA_GNSS.REC = 0;
    STA_GNSS.TMCNT_RADIO = 0;
    //MEMS
    STA_MEMS.CMD = 0;
    STA_MEMS.MSG = 1;
    STA_MEMS.RBCNT = 0;
    STA_MEMS.NFR = 0;
    STA_MEMS.TMCNT = 0;
    //高分相机
    STA_CIOMPCam.CMD = 0;
    STA_CIOMPCam.MSG = 1;
    STA_CIOMPCam.RBCNT = 0;
    STA_CIOMPCam.NFR = 0;
    //导航增强
    STA_NaviEnhance.CMD = 0;
    STA_NaviEnhance.MSG = 1;
    //CMOS相机
    STA_CMOS.CMD = 0;
    STA_CMOS.MSG = 1;
    //光电传感器
    STA_FotonAmur.CMD = 0;
    STA_FotonAmur.MSG = 1;
    //星敏Y
    STA_SSY.CMD = 0;
    STA_SSY.HL_TXF = 1;
    STA_SSY.MSG = 1;
    STA_SSY.RBCNT = 0;
    STA_SSY.NFR = 0;
    STA_SSY.FSM = 0;
    STA_SSY.REC = 0;
    //星敏Z
    STA_SSZ.CMD = 0;
    STA_SSZ.HL_TXF = 1;
    STA_SSZ.MSG = 1;
    STA_SSZ.RBCNT = 0;
    STA_SSZ.NFR = 0;
    STA_SSZ.FSM = 0;
    STA_SSZ.REC = 0;
    //飞轮XYZS
    for (cnt = 0; cnt<4; cnt++)
    {
        STA_FW[cnt].CMD = 0;
        STA_FW[cnt].HL_TXF = 1;
        STA_FW[cnt].MSG = 1;
        STA_FW[cnt].RBCNT = 0;
        STA_FW[cnt].NFR = 0;
        STA_FW[cnt].REC = 0;
    }
    //光纤陀螺
    STA_YH50.CMD = 0;
    STA_YH50.HL_TXF = 1;
    STA_YH50.MSG = 1;
    STA_YH50.RBCNT = 0;
    STA_YH50.NFR = 0;
    //磁强计
    STA_MAGME.CMD = 0;
    STA_MAGME.MSG = 1;
    STA_MAGME.HL_TXF = 1;
    STA_MAGME.RBCNT = 0;
    STA_MAGME.NFR = 0;
    //太敏
    STA_SUNS.CMD = 0;
    STA_SUNS.MSG = 1;
    STA_SUNS.HL_TXF = 0;
    STA_SUNS.RBCNT = 0;
    STA_SUNS.NFR = 0;
    STA_SUNS.REC = 0;
    //PCDU
    STA_EPS.CMD = 0;
    STA_EPS.MSG[0] = 1;
    STA_EPS.MSG[1] = 1;
    STA_EPS.RBCNT[0] = 0;
    STA_EPS.RBCNT[1] = 0;
    STA_EPS.RBCNT[2] = 0;

    //数传
    STA_DT.boottime = 0;
    STA_DT.ENWDT = 0XAA;

    test_bit = 0;
}
